#include <allegro.h>
#include "player.h"
#include "object.h"

player::player(int screen_w, int screen_h, int speed_x, char* nome) {
	this->img = load_bitmap(nome, NULL);

	this->w = img->w;
	this->h = img->h;

	this->x = screen_w / 2 - this->w / 2;
	this->y = screen_h - this->h;

	this->speed_x = speed_x;

}

void player::move(int scr_w) {
	if (key[KEY_LEFT]) {
		this->x -= this->speed_x;

		if (this->x < 0) {
			this->x = 0;
		}
	}

	else if (key[KEY_RIGHT]) {
		this->x += this->speed_x;

		if (this->x + this->w > scr_w) {
			this->x = scr_w - this->w;
		}
	}
}

void player::draw(BITMAP* buffer) {
	draw_sprite(buffer, this->img, this->x, this->y);
}

bool player::can_catch(object o) {
	if (((this->x + this->w > o.x) && (this->x < o.x + o.w)) && ((this->y + this->h > o.y) && (this->y < o.y + o.h)))
		return true;

	return false;
}

